﻿using System.Collections;
using System.Collections.Generic;
using GameFramework;
using GameFramework.Procedure;
using UnityEngine;
using UnityGameFramework.Runtime;
using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;
using GameFramework.Event;

public class ProcedureChangeScene : ProcedureBase
{

	private LoadingPanel loadingPanel;
	float loadingProgress;
    private bool loadSceneSuccess = false;

	protected override void OnEnter (ProcedureOwner procedureOwner)
	{
		base.OnEnter (procedureOwner);
        //		GameEntry.Event.Subscribe(LoadSceneSuccessEventArgs.EventId, OnLoadSceneSuccess);
        //		GameEntry.Event.Subscribe(LoadSceneFailureEventArgs.EventId, OnLoadSceneFailure);
        //		GameEntry.Event.Subscribe(LoadSceneUpdateEventArgs.EventId, OnLoadSceneUpdate);
        //		GameEntry.Event.Subscribe(LoadSceneDependencyAssetEventArgs.EventId, OnLoadSceneDependencyAsset);

        SceneComponent scene = UnityGameFramework.Runtime.GameEntry.GetComponent<SceneComponent>();

        //先卸载之前得场景
        string[] loadedSceneAssetNames = scene.GetLoadedSceneAssetNames();//获取所有当前已经加载的场景
        for (int i = 0; i < loadedSceneAssetNames.Length; i++)
        {
            scene.UnloadScene(loadedSceneAssetNames[i]);//UnloadScene卸载场景的方法
        }

        EventComponent Event = UnityGameFramework.Runtime.GameEntry.GetComponent<EventComponent>();
        Event.Subscribe(LoadSceneUpdateEventArgs.EventId, LoadSceneUpdateEvent);//订阅（监听）事件
	    Event.Subscribe(LoadSceneSuccessEventArgs.EventId, LoadSceneSuccess);
        Event.Subscribe(OpenUIFormSuccessEventArgs.EventId, OnOpenUIFormSuccess);
        //加载UI组件
        UIComponent UI = UnityGameFramework.Runtime.GameEntry.GetComponent<UIComponent>();
        //加载UI
        UIFormConfig uiFormCfg = UIFormConfig.GetRowById((int)UIForms.Loading);
        UI.OpenUIForm(AppConsts.UIResPath + uiFormCfg.AssetName, uiFormCfg.UIGroupName, this);

        // scene.LoadScene("CrateRole", this);//把场景名称传递进来
        //此处加载下一个场景
        scene.LoadScene(scene.m_NextScene, this);
    }
	protected override void OnLeave (ProcedureOwner procedureOwner, bool isShutdown)
	{
		base.OnLeave (procedureOwner, isShutdown);
		EventComponent Event = UnityGameFramework.Runtime.GameEntry.GetComponent<EventComponent>();
		Event.Unsubscribe (LoadSceneUpdateEventArgs.EventId,LoadSceneUpdateEvent);

        //关闭Loading界面
        UIComponent UI = UnityGameFramework.Runtime.GameEntry.GetComponent<UIComponent>();
        UIFormConfig uiFormCfg = UIFormConfig.GetRowById((int)UIForms.Loading);
        UIForm uf = UI.GetUIForm(AppConsts.UIResPath + uiFormCfg.AssetName);
        UI.CloseUIForm(uf);

    }

    protected override void OnUpdate(ProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds)
    {
        base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
        if(loadSceneSuccess)
            ChangeState<ProcedureGame>(procedureOwner);
    }

	private void LoadSceneUpdateEvent(object sender, GameEventArgs e)
	{
		LoadSceneUpdateEventArgs ne = (LoadSceneUpdateEventArgs)e;
		// 判断userData是否为自己
//		if (ne.UserData != this)
//		{
//			return;
//		}
		loadingProgress = ne.Progress;
		Log.Info("场景加载进度："+ne.Progress);
        if (loadingPanel != null)
            loadingPanel.LoadingProgress(ne.Progress);

	}


	private void OnOpenUIFormSuccess(object sender,GameEventArgs e)
	{
		OpenUIFormSuccessEventArgs ne = (OpenUIFormSuccessEventArgs)e;
        //loadingPanel = (LoadingPanel)ne.UIForm.Logic;
        //if (loadingPanel != null)
        //    loadingPanel.LoadingProgress (loadingProgress);
	}

    private void LoadSceneSuccess(object sender, GameEventArgs e)
    {
        loadSceneSuccess = true;
    }
    /*
        yield return new WaitForSeconds(2);
        //加载UI组件
        UIComponent UI = UnityGameFramework.Runtime.GameEntry.GetComponent<UIComponent>();
        //关闭UI

        UIFormConfig uiFormCfg = UIFormConfig.Get((int)UIForms.Loading);
        UIForm uf = UI.GetUIForm(AppConsts.UIResPath + uiFormCfg.AssetName);
        //while (uf == null)
        //{
        //    uf = UI.GetUIForm(AppConsts.UIResPath + uiFormCfg.AssetName);
        //}
        UI.CloseUIForm(uf);
     */
}

